package Ep06_BinaryTree.BT16_0513;

import Ep06_BinaryTree.TreeNode;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * Mine
 *
 * @author Jimmy
 * @date 2023/5/10 14:28
 * @description: 力扣 0513 最底层、最左边的叶子节点的值
 * 层序遍历：性能有点垃
 */
public class LevelOrder {
    public int findBottomLeftValue(TreeNode root) {
        int resVal = 0;

        if (root.left == null && root.right == null) return root.val;
        Deque<TreeNode> deque = new LinkedList<>();

        deque.offer(root);
        while(!deque.isEmpty()){
            List<Integer> list = new LinkedList<>();
            int size = deque.size();
            while(size > 0){
                TreeNode tempNode = deque.poll();
                list.add(tempNode.val);
                if (tempNode.left != null) deque.offer(tempNode.left);
                if (tempNode.right != null) deque.offer(tempNode.right);
                size--;
            }
            resVal = list.get(0);
        }
        return resVal;
    }
}
